package com.quectel.sdk.hikisc;

import com.quectel.sdk.ex.hikisc.HikFactoryErrorException;

/**
 * 海康实例建造器
 *
 * @author: rananxin
 * @email: ryan.ran@quectel.com
 * @date: 2022-10-19 11:01:00
 */
public class HikClientBuilder {

//    private static final Cache<String, HikIscRestUtils> CACHE = CacheBuilder.newBuilder().expireAfterAccess(10, TimeUnit.MINUTES).build();

    /**
     * 海康isc地址
     */
    private String host;
    /**
     * appKey访问凭证
     */
    private String ak;
    /**
     * secretKey访问凭证
     */
    private String sk;

    private HikClientBuilder() {
    }

    public static HikClientBuilder builder() {
        return new HikClientBuilder();
    }

    public HikClientBuilder host(String host) {
        this.host = host;
        return this;
    }

    public HikClientBuilder ak(String ak) {
        this.ak = ak;
        return this;
    }

    public HikClientBuilder sk(String sk) {
        this.sk = sk;
        return this;
    }

    public <T extends AbstractHikBaseClient> T build(Class<T> executorCls) {
        if (host == null || ak == null || sk == null) {
            throw new HikFactoryErrorException("Missing request parameter.");
        }
        try {
            T t = executorCls.newInstance();
//            t.setPetitioner(CACHE.asMap().computeIfAbsent(ak, s -> HikIscRestUtils.getInstance(ak, sk, host)));
            t.setRequester(HikISCRequester.getInstance(ak, sk, host));
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
